package com.alliky.classify.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alliky.classify.R;
import com.alliky.classify.R2;
import com.alliky.classify.mvp.contract.ClassifyContract;
import com.alliky.classify.mvp.presenter.ClassifyPresenter;
import com.alliky.core.base.Fragment;
import com.alliky.core.entity.vo.CategoryVO;
import com.alliky.core.entity.vo.GoodsVO;
import com.alliky.core.net.ImageLoader;
import com.alliky.core.router.RoutePath;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

@Route(path = RoutePath.Classify.CLASSIFY_FRAGMENT)
public class ClassifyFragment extends Fragment<ClassifyPresenter> implements ClassifyContract.View {

    BaseQuickAdapter<GoodsVO, BaseViewHolder> mAdapter;
    BaseQuickAdapter<CategoryVO, BaseViewHolder> mCategoryAdapter;

    @BindView(R2.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R2.id.recyclerViewTitle)
    RecyclerView recyclerViewTitle;
    @BindView(R2.id.recyclerViewContent)
    RecyclerView recyclerViewContent;

    @Override
    protected ClassifyPresenter createPresenter() {
        return new ClassifyPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.fragment_classify;
    }

    @Override
    public void onInitView(Bundle savedInstanceState, View view) {
        initRecyclerViewCategory();
        initRecyclerView();
        initRefreshLayout();
    }

    @Override
    public void onInitData() {
        super.onInitData();
        getCategory();
        getHotGoods(true);
    }

    private void initRecyclerViewCategory() {
        recyclerViewTitle.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerViewTitle.setAdapter(mCategoryAdapter = new BaseQuickAdapter<CategoryVO, BaseViewHolder>(R.layout.item_category_left_layout) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, CategoryVO categoryVO) {
                holder.setText(R.id.tv_name, categoryVO.getCate_name());
            }
        });
    }

    private void initRecyclerView() {
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recyclerViewContent.setLayoutManager(layoutManager);
        recyclerViewContent.setAdapter(mAdapter = new BaseQuickAdapter<GoodsVO, BaseViewHolder>(R.layout.item_category_content_layout) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, GoodsVO goodsVO) {
                ImageLoader.load(goodsVO.getGoods_thumb(), holder.getView(R.id.iv_image));
                holder.setText(R.id.tv_name, goodsVO.getGoods_name());
                holder.setText(R.id.tv_price, "￥" + goodsVO.getMarket_price());
            }
        });
    }

    private void initRefreshLayout() {
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getHotGoods(false);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getHotGoods(true);
            }
        });
    }

    private void getHotGoods(boolean isRefresh) {
        pageNum = isRefresh ? 1 : pageNum;
        HashMap params = new HashMap();
        params.put("type", 1);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        mPresenter.getHotGoods(params, isRefresh);
    }

    private void getCategory() {
        HashMap params = new HashMap();
        params.put("type", 1);
        params.put("pageNum", 1);
        params.put("pageSize", 4);
        mPresenter.getCategory(params);
    }

    @Override
    public void showCategory(List<CategoryVO> list) {
        mCategoryAdapter.setNewData(list);
    }

    @Override
    public void showHotGoods(List<GoodsVO> list, boolean isRefresh) {
        if (isRefresh) {
            mAdapter.setNewData(list);
        } else {
            mAdapter.addData(list);
        }
    }


    @Override
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    @Override
    public void finishLoadMore() {
        refreshLayout.finishLoadMore();
    }

    @Override
    public void finishRefresh() {
        refreshLayout.finishRefresh();
    }
}


